| Conditions | 48 |
| Paths | 3808 |
| Total Lines | 227 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 19 | ||
| Bugs | 1 | Features | 3 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like map.js ➔ initialize often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /*jslint |
||
| 498 | function initialize(xcenter, xzoom, xmap, xfeatures, xmarkers, xlines, xgeocache) { |
||
| 499 | 'use strict'; |
||
| 500 | |||
| 501 | var center = null; |
||
|
|
|||
| 502 | var atDefaultCenter = false; |
||
| 503 | var zoom = parseInt(xzoom, 10); |
||
| 504 | var maptype = xmap; |
||
| 505 | |||
| 506 | // parse markers |
||
| 507 | var markerdata = parseMarkersFromUrl(xmarkers); |
||
| 508 | var markercenter = null; |
||
| 509 | var clat = 0; |
||
| 510 | var clon = 0; |
||
| 511 | if (markerdata.length > 0) { |
||
| 512 | markerdata.map(function (m) { |
||
| 513 | clat += m.coords.lat(); |
||
| 514 | clon += m.coords.lng(); |
||
| 515 | }); |
||
| 516 | markercenter = new google.maps.LatLng(clat / markerdata.length, clon / markerdata.length); |
||
| 517 | } |
||
| 518 | |||
| 519 | var loadfromcookies = false; |
||
| 520 | if (xcenter && xcenter !== '') { |
||
| 521 | center = parseCenterFromUrl(xcenter); |
||
| 522 | } else if (markercenter) { |
||
| 523 | center = markercenter; |
||
| 524 | } else { |
||
| 525 | loadfromcookies = true; |
||
| 526 | |||
| 527 | /* try to read coordinats from cookie */ |
||
| 528 | clat = get_cookie_float('clat', CLAT_DEFAULT); |
||
| 529 | clon = get_cookie_float('clon', CLON_DEFAULT); |
||
| 530 | if (clat == CLAT_DEFAULT && clon == CLON_DEFAULT) { |
||
| 531 | atDefaultCenter = true; |
||
| 532 | } |
||
| 533 | |||
| 534 | clat = repairLat(clat, CLAT_DEFAULT); |
||
| 535 | clon = repairLon(clon, CLON_DEFAULT); |
||
| 536 | center = new google.maps.LatLng(clat, clon); |
||
| 537 | |||
| 538 | zoom = get_cookie_int('zoom', ZOOM_DEFAULT); |
||
| 539 | maptype = get_cookie_string('maptype', MAPTYPE_DEFAULT); |
||
| 540 | } |
||
| 541 | |||
| 542 | if (!center) { |
||
| 543 | center = new google.maps.LatLng(CLAT_DEFAULT, CLON_DEFAULT); |
||
| 544 | atDefaultCenter = true; |
||
| 545 | } |
||
| 546 | |||
| 547 | zoom = repairZoom(zoom, ZOOM_DEFAULT); |
||
| 548 | maptype = repairMaptype(maptype, MAPTYPE_DEFAULT); |
||
| 549 | |||
| 550 | var myOptions = { |
||
| 551 | zoom: zoom, |
||
| 552 | center: center, |
||
| 553 | scaleControl: true, |
||
| 554 | streetViewControl: false, |
||
| 555 | mapTypeControlOptions: { mapTypeIds: ['OSM', 'OSM/DE', 'OCM', 'OUTD', 'TOPO', google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN] }, |
||
| 556 | mapTypeId: google.maps.MapTypeId.ROADMAP }; |
||
| 557 | |||
| 558 | map = new google.maps.Map(document.getElementById("themap"), myOptions); |
||
| 559 | |||
| 560 | map.mapTypes.set("OSM", osmProvider("OSM")); |
||
| 561 | map.mapTypes.set("OSM/DE", osmDeProvider("OSM/DE")); |
||
| 562 | map.mapTypes.set("OCM", ocmProvider("OCM")); |
||
| 563 | map.mapTypes.set("OUTD", thunderforestOutdoorsProvider("OUTD")); |
||
| 564 | map.mapTypes.set("TOPO", opentopomapProvider("TOPO")); |
||
| 565 | map.setMapTypeId(maptype); |
||
| 566 | |||
| 567 | Sidebar.init(map); |
||
| 568 | ExternalLinks.init(map); |
||
| 569 | Lines.init(map); |
||
| 570 | Geolocation.init(map); |
||
| 571 | Hillshading.init(map); |
||
| 572 | NPA.init(map); |
||
| 573 | CDDA.init(map); |
||
| 574 | Freifunk.init(map); |
||
| 575 | |||
| 576 | //boundariesLayer = new google.maps.ImageMapType({ |
||
| 577 | // getTileUrl: function(coord, zoom) { |
||
| 578 | // if (6 <= zoom && zoom <= 16) |
||
| 579 | // { |
||
| 580 | // return tileUrl("http://korona.geog.uni-heidelberg.de/tiles/adminb/?x=%x&y=%y&z=%z", ["dummy"], coord, zoom); |
||
| 581 | // } |
||
| 582 | // else |
||
| 583 | // { |
||
| 584 | // return null; |
||
| 585 | // } |
||
| 586 | // }, |
||
| 587 | // tileSize: new google.maps.Size(256, 256), |
||
| 588 | // name: "adminb", |
||
| 589 | // alt: "Administrative Boundaries", |
||
| 590 | // maxZoom: 16 }); |
||
| 591 | |||
| 592 | // Create div for showing copyrights. |
||
| 593 | copyrightDiv = document.createElement("div"); |
||
| 594 | copyrightDiv.id = "map-copyright"; |
||
| 595 | copyrightDiv.style.fontSize = "11px"; |
||
| 596 | copyrightDiv.style.fontFamily = "Arial, sans-serif"; |
||
| 597 | copyrightDiv.style.margin = "0 2px 2px 0"; |
||
| 598 | copyrightDiv.style.whiteSpace = "nowrap"; |
||
| 599 | copyrightDiv.style.background = "#FFFFFF"; |
||
| 600 | map.controls[google.maps.ControlPosition.BOTTOM_RIGHT].push(copyrightDiv); |
||
| 601 | |||
| 602 | map.setCenter(center, zoom); |
||
| 603 | |||
| 604 | google.maps.event.addListener(map, "center_changed", function() { storeZoom(); storeCenter(); okapi_schedule_load_caches(); }); |
||
| 605 | google.maps.event.addListener(map, "zoom_changed", function() { storeZoom(); storeCenter(); okapi_schedule_load_caches(); }); |
||
| 606 | google.maps.event.addListener(map, "maptypeid_changed", function(){ updateCopyrights()}); |
||
| 607 | |||
| 608 | if (loadfromcookies) { |
||
| 609 | var raw_ids = Cookies.get('markers'); |
||
| 610 | if (raw_ids != undefined) { |
||
| 611 | var ids = raw_ids.split(':'); |
||
| 612 | for (var i = 0; i != ids.length; ++i) { |
||
| 613 | var id = parseInt(ids[i], 10); |
||
| 614 | if (id === null || id < 0 || id >= 26*10) continue; |
||
| 615 | |||
| 616 | var raw_data = Cookies.get('marker' + id); |
||
| 617 | if (raw_data == undefined) continue; |
||
| 618 | |||
| 619 | var data = raw_data.split(':') |
||
| 620 | if (data.length != 3 && data.length != 4) continue; |
||
| 621 | |||
| 622 | var lat = parseFloat(data[0]); |
||
| 623 | if (lat < -90 || lat > 90) continue; |
||
| 624 | var lon = parseFloat(data[1]); |
||
| 625 | if (lon < -180 || lon > 180) continue; |
||
| 626 | var r = parseFloat(data[2]); |
||
| 627 | if (r < 0 || r > 100000000000) continue; |
||
| 628 | |||
| 629 | var name = null; |
||
| 630 | if (data.length == 4) { |
||
| 631 | if (/^([a-zA-Z0-9-_]*)$/.test(data[3])) { |
||
| 632 | name = data[3]; |
||
| 633 | } |
||
| 634 | } |
||
| 635 | |||
| 636 | newMarker(new google.maps.LatLng(lat, lon), id, r, name); |
||
| 637 | } |
||
| 638 | } |
||
| 639 | |||
| 640 | var raw_lines = Cookies.get('lines'); |
||
| 641 | if (raw_lines != undefined) { |
||
| 642 | var linesarray = raw_lines.split('*'); |
||
| 643 | for (var i = 0; i < linesarray.length; ++i) { |
||
| 644 | var line = linesarray[i].split(':'); |
||
| 645 | if (line.length != 2) continue; |
||
| 646 | |||
| 647 | var id1 = alpha2id(line[0]); |
||
| 648 | if (id1 != -1 && theMarkers.getById(id1).isFree()) { |
||
| 649 | id1 = -1; |
||
| 650 | } |
||
| 651 | var id2 = alpha2id(line[1]); |
||
| 652 | if (id2 != -1 && theMarkers.getById(id2).isFree()) { |
||
| 653 | id2 = -1; |
||
| 654 | } |
||
| 655 | |||
| 656 | Lines.newLine(id1, id2); |
||
| 657 | } |
||
| 658 | } |
||
| 659 | } else { |
||
| 660 | for (var i = 0; i < markerdata.length; ++i) { |
||
| 661 | newMarker(markerdata[i].coords, markerdata[i].id, markerdata[i].r, markerdata[i].name); |
||
| 662 | } |
||
| 663 | |||
| 664 | var raw_lines = xlines; |
||
| 665 | if (raw_lines != null) { |
||
| 666 | /* be backwards compatible */ |
||
| 667 | if (raw_lines.length == 3 |
||
| 668 | && raw_lines[0] >= 'A' && raw_lines[0] <= 'Z' |
||
| 669 | && raw_lines[1] == '*' |
||
| 670 | && raw_lines[2] >= 'A' && raw_lines[2] <= 'Z') { |
||
| 671 | raw_lines = raw_lines[0] + ':' + raw_lines[2]; |
||
| 672 | } |
||
| 673 | |||
| 674 | var linesarray = raw_lines.split('*'); |
||
| 675 | for (var i = 0; i < linesarray.length; ++i) { |
||
| 676 | var line = linesarray[i].split(':'); |
||
| 677 | if (line.length != 2) continue; |
||
| 678 | |||
| 679 | var id1 = alpha2id(line[0]); |
||
| 680 | if (id1 != -1 && theMarkers.getById(id1).isFree()) { |
||
| 681 | id1 = -1; |
||
| 682 | } |
||
| 683 | var id2 = alpha2id(line[1]); |
||
| 684 | if (id2 != -1 && theMarkers.getById(id2).isFree()) { |
||
| 685 | id2 = -1; |
||
| 686 | } |
||
| 687 | |||
| 688 | Lines.newLine(id1, id2); |
||
| 689 | } |
||
| 690 | } |
||
| 691 | } |
||
| 692 | |||
| 693 | okapi_show_cache = xgeocache; |
||
| 694 | Sidebar.restore(true); |
||
| 695 | if (xfeatures == '[default]') { |
||
| 696 | Hillshading.restore(false); |
||
| 697 | //restoreBoundaries(false); |
||
| 698 | restoreGeocaches(false); |
||
| 699 | NPA.toggle(false); |
||
| 700 | CDDA.toggle(false); |
||
| 701 | Freifunk.toggle(false); |
||
| 702 | } else { |
||
| 703 | Hillshading.toggle(xfeatures.indexOf('h') >= 0 || xfeatures.indexOf('H') >= 0); |
||
| 704 | //toggleBoundaries(xfeatures.indexOf('b') >= 0 || xfeatures.indexOf('B') >= 0); |
||
| 705 | okapi_toggle_load_caches(xfeatures.indexOf('g') >= 0 || xfeatures.indexOf('G') >= 0); |
||
| 706 | NPA.toggle(xfeatures.indexOf('n') >= 0 || xfeatures.indexOf('N') >= 0); |
||
| 707 | Freifunk.toggle(xfeatures.indexOf('f') >= 0 || xfeatures.indexOf('F') >= 0); |
||
| 708 | } |
||
| 709 | restoreCoordinatesFormat(0); |
||
| 710 | |||
| 711 | if (xgeocache != "") { |
||
| 712 | okapi_toggle_load_caches(true); |
||
| 713 | atDefaultCenter = false; |
||
| 714 | } |
||
| 715 | |||
| 716 | // update copyrights + gmap-stuff now, once the map is fully loaded, and in 1s - just to be sure! |
||
| 717 | updateCopyrights(); |
||
| 718 | google.maps.event.addListenerOnce(map, 'idle', function(){ updateCopyrights(); }); |
||
| 719 | setTimeout(function(){ updateCopyrights(); }, 1000); |
||
| 720 | |||
| 721 | //if (atDefaultCenter) { |
||
| 722 | // Geolocation.whereAmI(); |
||
| 723 | //} |
||
| 724 | } |
||
| 725 |